<?php
namespace Platform\Domain;

use Platform\Common\Functions;
use Platform\Model\SystemBanner as SystemBannerModel;
use PhalApi\Exception\BadRequestException;

class Banner extends Functions {
    //获取banner列表
    public function bannerList($where,$select,$order,$page,$perpage) {
        $model = new SystemBannerModel();
        $data = array();
        //需要判断是不是超级管理员，如果不是需要筛选出自己和自己创建的人的数据
        if( $this->tokenData['role_name'] != '超级管理员' ){
            $where['add_who'] = $this->tokenData['id'];
        }
        $where['delete_flag'] = 'N';
        $count = $model->count($where,'id');
        $list = $model->getList($where,array(),$select,$order,$page,$perpage);
        if( !empty( $list ) ){
            foreach ( $list as $k => $v ){
                $list[$k]['img_list'][] = $v['banner_url'];
            }
        }
        $data['items'] = $list;
        $data['total'] = $count;
        return $data;
    }
    //添加banner接口
    public function addBanner($insertData){
        $model = new SystemBannerModel();
        //获取固定的参数
        $insertData = $this->selfAddFields($insertData);
        $res = $model->insert($insertData);
        if( $res === false ){
            throw new BadRequestException('banner添加错误，请重试！');
        }
        return $res;
    }
    //获取banner详情
    public function bannerDetail($id,$select){
        $model = new SystemBannerModel();
        $bannerData = $model->getData(array('id'=>$id),array(),$select);
        if( empty($bannerData)||$bannerData['delete_flag']=='Y' ){
            throw new BadRequestException('商品不存在或已删除！');
        }
        return $bannerData;
    }
    //编辑提交banner
    public function updateBanner($id,$updateData){
        $model = new SystemBannerModel();
        //查询当前数据是否存在
        $goodsData = $model->get($id,'id,delete_flag');
        if( empty($goodsData) || $goodsData['delete_flag'] == 'Y' ){
            throw new BadRequestException('banner不存在或已删除！');
        }
        //获取固定的参数
        $updateData = $this->selfEditFields($updateData);
        $res = $model->update($id,$updateData);
        if( $res === false ){
            throw new BadRequestException('banner编辑提交错误，请联系管理员！');
        }
        return $res;
    }
    //删除
    public function deleteBanner($id){
        $model = new SystemBannerModel();
        $info = $model->getData(array('id'=>$id));
        if( empty( $info ) ){
            throw new BadRequestException('数据不存在！');
        }
        if( $info['delete_flag'] =='Y' ){
            throw new BadRequestException('数据已被删除！');
        }
        //获取固定的参数
        $updateData = $this->selfDeleteFields(array());
        $res = $model->updateAll(array('id'=>$id),$updateData);
        if( $res === false ){
            throw new BadRequestException('数据删除有误，请联系管理员！');
        }
        return $res;
    }
}
